


CC:=g++
CPPFLAGS:=
CFLAGS:=

COPY_FILE := cp

OUT_DIR_HEAD := include
OUT_DIR_LIBS := library
OUT_DIR_OBJS := objects

#OUT_DIR_HEAD := Outs/head
#OUT_DIR_LIBS := Outs/libs

#MakeInit := $(shell mkdir -p Outs)
#MakeInit := $(shell mkdir -p $(OUT_DIR_HEAD))
#MakeInit := $(shell mkdir -p $(OUT_DIR_LIBS))

# SUBDIRS := CoreModule Loader
SUBDIRS := $(wildcard Code/*/)
MAKE_DIR := $(MAKE_DIR)

CURRENT_DIR := $(shell pwd)/$(MAKE_DIR)
BUILD_DIR := $(BUILD_DIR)

# 	找到所有头文件
ALL_FILE_HEAD := $(shell find $(CURRENT_DIR) -name "*.h")
#	找到所有源文件
ALL_FILE_CODE := $(shell find $(CURRENT_DIR) -name "*.cpp")


#ALL_FILE_NAME_HEAD := $(notdir $(ALL_FILE_HEAD))
ALL_FILE_NAME_HEAD := $(ALL_FILE_HEAD:$(CURRENT_DIR)%=$(OUT_DIR_HEAD)/%)

#	创建静态库列表
ALL_FILE_NAME_SLIB := $(ALL_FILE_CODE:$(CURRENT_DIR)%=$(OUT_DIR_LIBS)/%)
ALL_FILE_NAME_SLIB := $(ALL_FILE_NAME_SLIB:.cpp=.a)

#	创建obj 文件列表
ALL_FILE_NAME_OBJS := $(ALL_FILE_NAME_SLIB:.a=.o)
ALL_FILE_NAME_OBJS := $(ALL_FILE_NAME_OBJS:$(OUT_DIR_LIBS)%=$(OUT_DIR_OBJS)/%)

#	静态库列表，追加lib
ALL_FILE_NAME_SLIB := $(ALL_FILE_NAME_SLIB:$(OUT_DIR_LIBS)/%=$(OUT_DIR_LIBS)/lib%)

#	生成每个对应的头文件
$(OUT_DIR_HEAD)/%.h : $(CURRENT_DIR)%.h
	@echo $(COPY_FILE) -f $< $@ 
	$(COPY_FILE) -f $< $@ 

#	生成每个obj 文件
$(OUT_DIR_OBJS)/%.o : $(CURRENT_DIR)%.cpp
	$(CC) $(CPPFLAGS) -c $< -o $@ -static

#	生成每个静态库文件
$(OUT_DIR_LIBS)/lib%.a : $(OUT_DIR_OBJS)/%.o
	@echo ar cr $@ $< 
	ar cr $@ $< 

# build 单一目录
build_dir: $(ALL_FILE_NAME_OBJS) $(ALL_FILE_NAME_HEAD)
#	@echo MAKE_DIR           : $(MAKE_DIR)
#	@echo CURRENT_DIR        : $(CURRENT_DIR)
#	@echo OUT_DIR_HEAD       : $(OUT_DIR_HEAD)
#	@echo OUT_DIR_LIBS       : $(OUT_DIR_LIBS)
#	@echo ALL_FILE_HEAD      : $(ALL_FILE_HEAD)
#	@echo ALL_FILE_CODE      : $(ALL_FILE_CODE)
#	@echo ALL_FILE_NAME_HEAD : $(ALL_FILE_NAME_HEAD)
#	@echo ALL_FILE_NAME_SLIB : $(ALL_FILE_NAME_SLIB)
	@echo build_dir end

# 清理单一目录
clear_dir:
#	@rm -rf $(OUT_DIR_HEAD)
#	@rm -rf $(OUT_DIR_LIBS)
#	@rm -rf Outs
	@echo clear_dir end


all:
	@mkdir -p $(OUT_DIR_HEAD)
	@mkdir -p $(OUT_DIR_LIBS)
	@mkdir -p $(OUT_DIR_OBJS)
#	build 全部，一个目录一个目录编译
#	@mkdir -p $(OUT_DIR_OBJS)
	for dir in $(SUBDIRS); do $(MAKE) build_dir MAKE_DIR=$$dir ; done
#	@rm -rf $(OUT_DIR_OBJS)


clear:
#	清理
#	for dir in $(SUBDIRS); do $(MAKE) clear_dir MAKE_DIR=$$dir ; done
#	清理所有内容
	@rm -rf $(OUT_DIR_HEAD)
	@rm -rf $(OUT_DIR_LIBS)
	@rm -rf $(OUT_DIR_OBJS)


build:
	@echo build success
